package com.cdsxt.base;

import java.net.URLDecoder;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cdsxt.util.ClassUtil;
import com.cdsxt.util.SpringUtil;


/**
 * 
 * @author 成都尚学堂 & mr zeng
 * 客服热线：028-65176856
 * 网址： www.cd-sxt.com
 * 
 * @param <PoCls>传入当前dao层对应的po类型
 */
@Transactional
public abstract  class BaseService<PoCls extends BaseEntity>{
	
	/**
	 * ==================================通用crud===========================================
	 */
	//基础的dao这里根据模块名注入了对应的dao
	private BaseDao<PoCls> baseDao;
	
	public BaseDao<PoCls> getBaseDao() {
		if (this.baseDao==null) {
			throw new RuntimeException("当前类没有设置泛型参数不能获取模块的dao");
		}
		return baseDao;
	}
	
	//baseController初始化方法：
	//定时器
	private static Timer tm =new Timer();
	//@PostConstruct
	public BaseService()  {
		//延时2000执行，防止SpringUtil.getBean拿不到数据
		tm.schedule(new TimerTask() {
			@Override
			public void run() {
				try {
					Class<Object> poCls = ClassUtil.getClassParamType(BaseService.this.getClass());
					String moduleName = poCls.getSimpleName().replaceAll("Po", "");
					//模块名
					moduleName= moduleName.substring(0, 1).toLowerCase()+moduleName.substring(1);
					
					//dao的bean对象的名
					String daoBeanName=moduleName+"Dao";
					//从springbean工厂获取dao的bean
					baseDao = (BaseDao<PoCls>) SpringUtil.getBean(daoBeanName);
				} catch (Exception e) {
					System.out.println(e.toString());
					System.out.println("当前类："+BaseService.this.getClass()+"没有设置泛型参数");
				}
			}
		}, 2000);
	}
	/**
	 * ==================================通用crud===========================================
	 */
	public <AnyReturnType,AnyParamType>  List<AnyReturnType> findListCusTypeByCondition(AnyParamType params){
		List<AnyReturnType> list = getBaseDao().findListCusTypeByCondition(params);
		return list;
	}
	
	public List<PoCls> findAll() {
		List<PoCls> list = getBaseDao().findAll();
		return list;
	}
	
	
	public List<PoCls> findByPage(Integer start, Integer limit) {
		List<PoCls> list = getBaseDao().findByPage(start, limit);
		return list;
	
	}
	
	public void save(PoCls po) {
		po.setCreateDate(new Date());
		getBaseDao().saveNotNullField(po);
	}	
	
	
	public void update(PoCls po) {
		po.setUpdateDate(new Date());
		getBaseDao().updateNotNullFieldById(po);
	}
	
	
	public void delete(Integer id) {
		getBaseDao().deleteById(id);
	}
	
	public PoCls findOne(Integer id) {
		return getBaseDao().findById(id);
	}
}
